package com.slt.concurrency.mashibing2019.juc.c_004_Volatile.my;

import java.util.ArrayList;
import java.util.List;

/**
 * volatile  禁止指令重排序
 *
 * DCL ： double check lock
 */
public class Volatile_sync {
    //构造器私有化 禁止在类外 new对象
    private int count = 0;

    private void countPlusPlus(){
        for (int i = 0; i < 10000; i++) {
            count++;
        }
    }


    public static void main(String[] args) {
        Volatile_sync v = new Volatile_sync();
        List<Thread> threadList = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            threadList.add(new Thread(v::countPlusPlus));
        }

        threadList.forEach(Thread::start);

        threadList.forEach(thread -> {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(v.count);
    }
}
